package mysql.mysqlPool;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;

/**
 * @author 宋鑫宇
 * @version 1.0
 * 使用数据库连接池：druid连接池
 */
public class mysqlDataSource {
    public static void main(String[] args) throws Exception{
        //1.导入jar包
        //2.定义配置文件
        //3.加载配置文件
        Properties prop = new Properties();
        prop.load(new FileInputStream("src/druid.properties"));
        //4.获取连接池对象
        DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
        //5.获取对应的数据库链接
        Connection conn = dataSource.getConnection();
        //6.编写sql语句
        String sql = "select * from stu";
        //7.对sql语句执行预编译
        PreparedStatement pre = conn.prepareStatement(sql);
        //8.执行sql语句
        ResultSet rs = pre.executeQuery();
        //9.处理结果
        while (rs.next()){
            System.out.println(rs.getString("name"));
        }
        //关闭资源
        rs.close();
        conn.close();
        pre.close();

    }
}
